package cn.dengta.webapp.media.entity;

import java.math.BigDecimal;
import java.util.*;

import cn.dengta.common.util.Common;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.im.enums.ChatType;
import cn.dengta.webapp.user.entity.Tag;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;
import org.apache.commons.lang3.tuple.Pair;

/**
 * 语音通话记录评价表
 *
 * @author lgy
 * @since 2023-02-16
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_media_eval_log")
public class MediaEvalLog extends TimeBasedEntity {

	/** 通话类型：2=语音通话；3=视频通话 {@link cn.dengta.webapp.im.enums.ChatType} */
	Integer type;
	/** 发起方用户ID */
	Long userId;
	/** 接听方用户ID */
	Long toUserId;
	/** 付费方用户ID */
	Long payUserId;
	/** 语音通话记录ID */
	Long mediaLogId;
	/** 通话开始时间 */
	Date beginTime;
	/** 通话开始时间 */
	Date endTime;
	/** 通话时长（ 秒 ） */
	Integer duration;
	/** 通话花费钻石 */
	Integer cost;
	/** 通话获得积分 */
	BigDecimal integral;
	/** 发起方评分 */
	Integer score;
	/** 接听方评分 */
	Integer toScore;
	/** 发起方标签 */
	String tags;
	/** 接收方标签 */
	String toTags;
	/** 发起方备注 */
	String remark;
	/** 接收方备注 */
	String toRemark;

	public MediaEvalLog init(MediaLog log, Date now) {
		this.type = log.getType();
		this.userId = log.getFromUserId();
		this.toUserId = log.getToUserId();
		this.payUserId = log.getPayUserId();
		this.mediaLogId = log.getId();
		this.beginTime = log.getBeginTime();
		this.endTime = log.getEndTime();
		this.duration = log.getDuration();
		this.cost = log.getCost();
		this.tags = this.toTags = this.remark = this.toRemark = "";
		super.initTime(now);
		return this;
	}

	public String tagsLabel(Map<Long, String> tagMap) {
		return StringUtil.notEmpty(tags) && X.isValid(tagMap) ? StringUtil.join(Common.splitAsLongList(tags), (sb, t) -> sb.append(tagMap.get(t)), Common.SEP) : tags;
	}

	public String toTagsLabel(Map<Long, String> tagMap) {
		return StringUtil.notEmpty(toTags) && X.isValid(tagMap) ? StringUtil.join(Common.splitAsLongList(toTags), (sb, t) -> sb.append(tagMap.get(t)), Common.SEP) : toTags;
	}

	public MediaEvalLog add(boolean fromOrTo, Integer score, String tags, String remark) {
		if (fromOrTo) {
			this.score = score;
			this.tags = StringUtil.toString(tags);
			this.remark = StringUtil.toString(remark);
			return this;
		}
		this.toScore = score;
		this.toTags = StringUtil.toString(tags);
		this.toRemark = StringUtil.toString(remark);
		return this;
	}

	/** Pair<付费方, 收益方> */
	public Pair<Long, Long> payUserPair() {
		return Pair.of(Objects.equals(payUserId, userId) ? userId : toUserId, Objects.equals(payUserId, userId) ? toUserId : userId);
	}

	public Tag.TagType tagType() {
		return type == ChatType.AUDIO.ordinal() ? Tag.TagType.AUDIO_EVAL : Tag.TagType.VIDEO_EVAL;
	}

	public static final String TYPE = "type";
	public static final String USER_ID = "user_id";
	public static final String TO_USER_ID = "to_user_id";
	public static final String PAY_USER_ID = "pay_user_id";
	public static final String MEDIA_LOG_ID = "media_log_id";
	public static final String BEGIN_TIME = "begin_time";
	public static final String END_TIME = "end_time";
	public static final String DURATION = "duration";
	public static final String COST = "cost";
	public static final String SCORE = "score";
	public static final String TO_SCORE = "to_score";
	public static final String TAGS = "tags";
	public static final String TO_TAGS = "to_tags";
	public static final String REMARK = "remark";
	public static final String TO_REMARK = "to_remark";

}
